﻿/**
 * 邮箱正确性验证
 * @param val
 * @returns {Boolean}
 */
var emailValue="";
function isEmailCheck(val){
	var arrs=val.split(";");
	var match=/^\s*\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*(\;\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)*(\;)*\s*$/;
	for(var i=0;i<arrs.length;i++){
		if(arrs[i]=="[E_mail]"||arrs[i]=="[收件人邮箱]"){
			continue;
		}
		//var temp=arrs[i].toLowerCase();
		if(!match.test(arrs[i])){
			emailValue=arrs[i];
			return true;
		}
	}
	return false;
}
/**
 * 邮件、短信设置
 */
Ext.define(
				'HIT.controller.SetMail',
				{
					extend : 'Ext.app.Controller',
					views : [ 'SetMailTemplatePanel' ],

					onButtonClick : function(button, e, options) {
						if (button.id == 'set-mail') {
							var tabpanelId = 'tab-set-mail';
							var tab = Ext.getCmp(tabpanelId);
							if (tab == undefined) {
								return;
							}
							
							// Extjs 验证：收件人,cc,bcc,主题
				            var basicForm = tab.getForm();
				            if (!basicForm.isValid()) {
				                return;
				            }

				            // 获取页面对应控件的值
							// 通知设置
							var noticeTypeGroup = Ext.getCmp('noticeType');
							var templateType = '';
							noticeTypeGroup.items.each(function(item) {
								if (item.checked) {
									templateType = item.inputValue;
								}
							});

							// 收件人
							var receiver = Ext.getCmp('receiver').getValue();

							// CC
							var carbonCopy = Ext.getCmp('cc').getValue();

							// BCC
							var blindCarbonCopy = Ext.getCmp('bcc').getValue();
							
							if(isEmailCheck(receiver)){
								Ext.getCmp('receiver').markInvalid(Share.T(emailValue)+Share.T('该邮件格式无效'));
								emailValue="";
								//Ext.getCmp('receiver').markInvalid(Share.T('该邮件格式无效'));
								return;
							}
							if(isEmailCheck(carbonCopy)){
								//Ext.getCmp('cc').markInvalid(Share.T('该邮件格式无效'));
								Ext.getCmp('cc').markInvalid(Share.T(emailValue)+Share.T('该邮件格式无效'));
								emailValue="";
								return;
							}
							if(isEmailCheck(blindCarbonCopy)){
								//Ext.getCmp('bcc').markInvalid(Share.T('该邮件格式无效'));
								Ext.getCmp('bcc').markInvalid(Share.T(emailValue)+Share.T('该邮件格式无效'));
								emailValue="";
								return;
							}
                            

							// 主题
							var theme = Ext.getCmp('subject').getValue();

							// 邮件正文
							var mailContent = Ext.getCmp('emailBody').getValue();

							// 短信正文
							var messageContent = Ext.getCmp('messageBody').getValue();
							
							// 启用邮件通知
							var mailNoticeFlg = Ext.getCmp('mailNoticeFlg').getValue() == true ? 1 : 0;
							if (mailNoticeFlg == 1 && mailContent.trim().length < 1) {
								Share.ShowMessage("I0010","邮件正文");
								return;
							}

							// 启用短信通知
							var messageNoticeFlg = Ext.getCmp('messageNoticeFlg').getValue() == true ? 1 : 0;
							if (messageNoticeFlg == 1 && messageContent.trim().length < 1) {
								Share.ShowMessage("I0010","短信正文");
								return;
							}

							// 更新模板
							Share.AjaxRequest({
								url : ctx + "/updateTemplate",
								params : {
									templateType : templateType,
									receiver : receiver,
									carbonCopy : carbonCopy,
									blindCarbonCopy : blindCarbonCopy,
									theme : theme,
									mailContent : mailContent,
									messageContent : messageContent,
									mailNoticeFlg : mailNoticeFlg,
									messageNoticeFlg : messageNoticeFlg
								},
								showMsg : true
							});
						}
					},
					init : function(application) {

						this.control({
							"#headpanel button" : {
								click : this.onButtonClick
							}
						});
					}
				});